package com.nova.android.tvlauncher.ext

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.TextView
import androidx.core.view.isVisible


fun View.toggle16to9Scale(
    isFirstItem: Boolean = false,
    isFocused: Boolean,
    scaleFactor: Float = 1.3f
) {
    // 设置缩放中心
    pivotX = if (isFirstItem) 0f else width / 2f // 第一个 Item 左侧固定，非第一个中心缩放
    pivotY = height / 2f // Y 轴始终以中心为支点

    // 根据焦点状态选择缩放比例
    val targetScale = if (isFocused) scaleFactor else 1.0f

    // 创建X轴和Y轴缩放动画
    val scaleX = ObjectAnimator.ofFloat(this, View.SCALE_X, targetScale)
    val scaleY = ObjectAnimator.ofFloat(this, View.SCALE_Y, targetScale)

    // 使用AnimatorSet组合动画
    AnimatorSet().apply {
        playTogether(scaleX, scaleY)
        duration = 200
        start()
    }
}

fun View.scaleAnimation(hasFocus: Boolean, scale: Float = 1.15f,animDuration:Long = 300) {
    // 根据焦点状态选择缩放比例
    val targetScale = if (hasFocus) scale else 1.0f

    // 创建X轴和Y轴缩放动画
    val scaleX = ObjectAnimator.ofFloat(this, View.SCALE_X, targetScale)
    val scaleY = ObjectAnimator.ofFloat(this, View.SCALE_Y, targetScale)

    // 使用AnimatorSet组合动画
    AnimatorSet().apply {
        playTogether(scaleX, scaleY)
        duration = animDuration
        start()
    }
}


fun View.animSlide() {
    if (isVisible) isVisible = false

    visibility = View.VISIBLE
    animate()
        .translationX(0f) // 初始位置（从右侧滑入）
        .withStartAction { translationX = width.toFloat() } // 起始位置设为屏幕右侧
        .setDuration(500) // 滑入动画时长
        .withEndAction {
            // 延迟2500ms后滑出
            postDelayed({
                animate()
                    .translationX(-width.toFloat()) // 滑出到左侧
                    .setDuration(500)
                    .withEndAction { visibility = View.GONE }
                    .start()
            }, 3000)
        }
        .start()
}

fun TextView.setTextWithSlideHorizontal(newText: String, aniDuration: Long = 500, slideDistance: Float = 30f) {
    val slideOut = ObjectAnimator.ofFloat(this, View.TRANSLATION_X, 0f, -slideDistance)
    val fadeOut = ObjectAnimator.ofFloat(this, View.ALPHA, 1f, 0f)

    val slideIn = ObjectAnimator.ofFloat(this, View.TRANSLATION_X, slideDistance, 0f)
    val fadeIn = ObjectAnimator.ofFloat(this, View.ALPHA, 0f, 1f)

    val outSet = AnimatorSet().apply {
        playTogether(slideOut, fadeOut)
       duration = aniDuration
    }

    val inSet = AnimatorSet().apply {
        playTogether(slideIn, fadeIn)
        duration = aniDuration
    }

    outSet.addListener(object : AnimatorListenerAdapter() {
        override fun onAnimationEnd(animation: Animator) {
            this@setTextWithSlideHorizontal.text = newText
            inSet.start()
        }
    })

    outSet.start()
}

// 在类顶部添加动画工具函数M
 fun View.fadeView( show: Boolean, duration: Long = 300) {
    this.clearAnimation()
    val animator = if (show) {
        this.alpha = 0f
        this.isVisible = true
        this.animate().alpha(1f).setDuration(duration)
    } else {
        this.animate().alpha(0f).setDuration(duration).withEndAction {
            this.isVisible = false
        }
    }
    animator.start()
}

